package chunithmlauncher;

import chunithmlauncher.option.ChunithmOption;
import chunithmlauncher.utils.JsonUtil;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.springframework.context.annotation.Bean;

import java.io.IOException;

/**
 * @author Reabin
 * @date 2021/10/21
 **/

@FXMLController
public class MainController {
    public static ChunithmOption chunithmOption;

    @Bean
    public void loadConfig(){
        String userdir = System.getProperty("user.dir").replace("\\", "/")+"/config.json";;
        chunithmOption=JsonUtil.jsonFile2Object(userdir, ChunithmOption.class);
        System.out.println(userdir);
    }

    public void startGame(final Event event){
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
            process = runtime.exec(chunithmOption.getStartbat());
        }catch (Exception e){
            System.out.println("错误！");
        }
        /*System.out.println("开始游戏");
        JIntellitype.getInstance().registerHotKey(1, JIntellitype.MOD_ALT, (int)'S');
        JIntellitype.getInstance().registerHotKey(0, JIntellitype.MOD_ALT, (int)'Q');
        System.out.println("开始监听全局键盘");
        JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {
            @Override
            public void onHotKey(int markCode) {
                switch (markCode) {
                    case 1:
                        Runtime runtime =Runtime.getRuntime();
                        Process process = null;
                        try{
                            process = runtime.exec("taskkill /f /im Bandizip.exe");
                        }catch (Exception e){
                            System.out.println("错误!");
                        }
                        System.out.println("关闭成功！");
                        showMessage();
                        break;
                    case 0:
                        System.exit(0);
                        break;
                }
            }
        });*/
    }
    @FXML
    public void stopGame(final Event event) throws IOException{

        showOption();
    }


    public void showOption()throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/chunithmlauncher/tool.fxml"));
        Parent root =loader.load();
        Scene scene = new Scene(root);
        ToolController controller = loader.getController();
        controller.initData(chunithmOption.getStartbat());
        Stage primaryStage = new Stage();
        primaryStage.initModality(Modality.APPLICATION_MODAL);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
